import java.sql.*;

public class Main {
    public static void main(String[] args) {
        // MySQL数据库信息
        /*String url = "jdbc:mysql://localhost:3306/mydatabase";
        String username = "root";
        String password = "password";*/

        String url = "jdbc:sqlserver://localhost:1433;database=AirLine;encrypt=true;trustServerCertificate=true";
        String username = "sa";
        String password = "123";
        try {
            // 加载驱动程序
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            // 建立与数据库的连接
            Connection connection = DriverManager.getConnection(url, username, password);

            //Statement 向数据库发送sql语句
            Statement statement=connection.createStatement();

            System.out.println("-----------第一次查询------------");
            //查询数据库信息
            //sql语句先用字符串存起来
            String query="select *from User_Password";
            //ResultSet 接收 Statement发送的查询语句返回的结果
            ResultSet resultSet=statement.executeQuery(query);
            while(resultSet.next())
            {
                String name=resultSet.getString("用户");
                String pass=resultSet.getString("密码");
                String grade=resultSet.getString("等级");
                System.out.println(name+"\t"+pass+"\t"+grade);
            }



            //插入
            String in="insert into User_Password values('用户6','123456789','6')";
            statement.executeUpdate(in);
            ResultSet resultSet1=statement.executeQuery(query);
            System.out.println("-----------插入后第二次查询------------");
            while(resultSet1.next())
            {
                String name=resultSet1.getString("用户");
                String pass=resultSet1.getString("密码");
                String grade=resultSet1.getString("等级");
                System.out.println(name+"\t"+pass+"\t"+grade);
            }



            //修改
            String edit="update User_Password set 用户='用户123' where 用户='用户6'";
            statement.executeUpdate(edit);
            ResultSet resultSet3=statement.executeQuery(query);
            System.out.println("-----------修改后第四次查询------------");
            while(resultSet3.next())
            {
                String name=resultSet3.getString("用户");
                String pass=resultSet3.getString("密码");
                String grade=resultSet3.getString("等级");
                System.out.println(name+"\t"+pass+"\t"+grade);
            }

            //删除
            String del="delete from User_Password where 用户='用户123'";
            statement.executeUpdate(del);
            ResultSet resultSet2=statement.executeQuery(query);
            System.out.println("-----------删除后第三次查询------------");
            while(resultSet2.next())
            {
                String name=resultSet2.getString("用户");
                String pass=resultSet2.getString("密码");
                String grade=resultSet2.getString("等级");
                System.out.println(name+"\t"+pass+"\t"+grade);
            }
            //关闭Statement
            statement.close();
            // 关闭连接
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}